package com.example.mybaseapp.ui

import android.app.Activity
import android.content.pm.PackageManager
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaPlayer.OnCompletionListener
import android.media.MediaRecorder
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.Button
import android.widget.ProgressBar
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.mybaseapp.R
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException



class Main14Activity : AppCompatActivity() {
    private var mProgressBar: ProgressBar? = null
    private var mBtn1: Button? = null
    private var mBtn2: Button? = null
    private var mBtn3: Button? = null
    private var mAudioRecord: AudioRecord? = null
    private var isRecording = false
    var laiyuan: Int = MediaRecorder.AudioSource.MIC //来源
    var samHz: Int = 16000 //采样频率
    var shengdao: Int = AudioFormat.CHANNEL_IN_MONO //声道
    var audioFormat: Int = AudioFormat.ENCODING_PCM_16BIT //格式
    var bufferSize: Int = 2 * AudioRecord.getMinBufferSize(samHz, shengdao, audioFormat) //缓冲区大小
    private val voicePath = Environment.getExternalStorageDirectory()
        .path + "/audio"
    private val voiceName = "/test.pcm"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main14)
//        init()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

//    private fun init() {
//        Main14Activity.Companion.verifyStoragePermissions(this)
//        mProgressBar = findViewById<ProgressBar>(R.id.progressbar)
//        mProgressBar.setVisibility(View.GONE)
//        mBtn1 = findViewById<Button>(R.id.btn1)
//        mBtn2 = findViewById<Button>(R.id.btn2)
//        mBtn3 = findViewById<Button>(R.id.btn3)
//
//        mBtn1.setOnClickListener(View.OnClickListener {
//            if (mAudioRecord == null) {
//                startRecord()
//            } else {
//            }
//        })
//
//        mBtn2.setOnClickListener(View.OnClickListener {
//            if (mAudioRecord != null) {
//                stopRecord()
//            }
//        })
//
//        mBtn3.setOnClickListener(View.OnClickListener {
//            MediaManager.playSound(voicePath,
//                OnCompletionListener { })
//        })
//    }

//    fun startRecord() {
//        mAudioRecord = AudioRecord(
//            laiyuan, samHz,
//            shengdao, audioFormat, bufferSize
//        )
//
//        val data = ByteArray(bufferSize)
//        val file = File(voicePath)
//        val fileaudio = File(voicePath + voiceName)
//
//        if (fileaudio.exists()) {
//            fileaudio.delete()
//        }
//
//        if (!file.mkdirs()) {
//            return
//        }
//
//        mAudioRecord!!.startRecording()
//        isRecording = true
//        mProgressBar!!.visibility = View.VISIBLE
//
//        Thread {
//            var fos: FileOutputStream? = null
//            try {
//                fos = FileOutputStream(fileaudio)
//            } catch (e: FileNotFoundException) {
//                e.printStackTrace()
//            }
//            if (null != fos) {
//                while (isRecording) {
//                    val read = mAudioRecord!!.read(data, 0, bufferSize)
//                    //返回正确时才读取数据
//                    if (AudioRecord.ERROR_INVALID_OPERATION != read) {
//                        try {
//                            fos.write(data)
//                        } catch (e: IOException) {
//                            e.printStackTrace()
//                        }
//                    }
//                }
//                try {
//                    fos.close()
//                } catch (e: IOException) {
//                    e.printStackTrace()
//                }
//            }
//        }.start()
//    }
//
//
//    fun stopRecord() {
//        isRecording = false
//        if (mAudioRecord != null) {
//            mProgressBar!!.visibility = View.GONE
//            mAudioRecord!!.stop()
//            mAudioRecord!!.release()
//            //调用release之后必须置为null
//            mAudioRecord = null
//        }
//    }
//
//    companion object {
//        private const val REQUEST_EXTERNAL_STORAGE = 1
//        private val PERMISSIONS_STORAGE = arrayOf(
//            "android.permission.READ_EXTERNAL_STORAGE",
//            "android.permission.WRITE_EXTERNAL_STORAGE",
//            "android.permission.RECORD_AUDIO"
//        )
//
//        fun verifyStoragePermissions(activity: Activity?) {
//            try {
//                //检测是否有写的权限
//                val permission1 = ActivityCompat.checkSelfPermission(
//                    activity!!,
//                    "android.permission.WRITE_EXTERNAL_STORAGE"
//                )
//                val permission2 = ActivityCompat.checkSelfPermission(
//                    activity,
//                    "android.permission.RECORD_AUDIO"
//                )
//                if (permission1 != PackageManager.PERMISSION_GRANTED || permission2 != PackageManager.PERMISSION_GRANTED) {
//                    // 没有写的权限，去申请写的权限，会弹出对话框
//                    ActivityCompat.requestPermissions(
//                        activity,
//                        Main14Activity.Companion.PERMISSIONS_STORAGE,
//                        Main14Activity.Companion.REQUEST_EXTERNAL_STORAGE
//                    )
//                }
//            } catch (e: Exception) {
//                e.printStackTrace()
//            }
//        }
//    }
}
